package logic.util;

import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.ciko.code.bean.User;
import com.ciko.code.http.BaseResponse;
import com.ciko.code.http.HttpClient;
import com.ciko.code.http.HttpResponseListener;
import com.ciko.code.http.Req.UserLoginReq;
import com.mywork.WorkUIKit;
import com.shwread.android.qysw.activity.MainActivity;
import com.shwread.android.qysw.activity.NewLoginActivity;

import logic.constants.DefaultConsts;

/**
 * 项目名称：association
 * 包名称：logic.util
 * 类描述：获取太保账号信息
 * 创建人： wayne
 * 创建时间 2016/1/28 15:49.
 */
public class TaiBaoLogin {


    public void taibaoLogin(final NewLoginActivity context, String account) {
        //获取太保账号信息
        HttpClient.getInstance(context).post(new UserLoginReq(account), new HttpResponseListener() {

            @Override
            public void onRequestStart(String tag) {

            }

            @Override
            public void onResponseSuccess(BaseResponse<?> response, String tag) {
                BaseResponse obj = (BaseResponse) response;
                Log.e("Success", obj.getResultCode() + "");
                if (obj.getResultCode() == 1003) {
                    String StrObject = (String) obj.getReturnObject();
                    Log.e("StrObject", StrObject + "");
                    Intent intent = new Intent();
                    intent.setAction(DefaultConsts.EXCEPTION_LOGIN_STRING);
                    intent.putExtra("json", "1003");
                    intent.putExtra("packageName", AppUtils.getPackageName(context));
                    context.sendBroadcast(intent);
                } else {
                    User user = (User) response.getReturnObject();
                    WorkUIKit.setUser(user);
                    Intent intent = new Intent(context, MainActivity.class);
                    intent.setAction(MainActivity.TO_ENTERPRISE_ACTION);
                    context.startActivity(intent);
                    context.finish();
//                    if (HealthyConfig.isUseStep) {
//                        QyreaderPerferencesUtil util = QyreaderPerferencesUtil.getInstance(context, DefaultConsts.FIRST_TAG);
//                        if (util.getShowHealthHelp()) {
//                            util.setShowHealthHelp(false);
//                            Util.showToast(context, "计步功能已经同步开启");
//                        }
//                    }
                }
            }

            @Override
            public void onResponseFail(String tag, int resultCode) {
//                Util.showToast(context, "获取太保账号异常");
            }
        });
    }

    public void taibaoLogin(final Context context, String account) {
        //获取太保账号信息
        HttpClient.getInstance(context).post(new UserLoginReq(account), new HttpResponseListener() {

            @Override
            public void onRequestStart(String tag) {

            }

            @Override
            public void onResponseSuccess(BaseResponse<?> response, String tag) {
                BaseResponse obj = (BaseResponse) response;
                Log.e("Success", obj.getResultCode() + "");
                if (obj.getResultCode() == 1003) {
                    String StrObject = (String) obj.getReturnObject();
                    Log.e("StrObject", StrObject + "");
                    Intent intent = new Intent();
                    intent.setAction(DefaultConsts.EXCEPTION_LOGIN_STRING);
                    intent.putExtra("json", "1003");
                    intent.putExtra("packageName", AppUtils.getPackageName(context));
                    context.sendBroadcast(intent);
                } else {
                    User user = (User) response.getReturnObject();
                    WorkUIKit.setUser(user);
                }
            }

            @Override
            public void onResponseFail(String tag, int resultCode) {
//                Util.showToast(context, "获取太保账号异常");
            }
        });
    }

    /**
     * 闪屏界面调用登录太保方法
     */
    public void LogintaibaoLogin(final Context context, String account,final LoginTaiBaoListener loginTaiBaoListener) {
        //获取太保账号信息
        HttpClient.getInstance(context).post(new UserLoginReq(account), new HttpResponseListener() {

            @Override
            public void onRequestStart(String tag) {

            }

            @Override
            public void onResponseSuccess(BaseResponse<?> response, String tag) {
                BaseResponse obj = (BaseResponse) response;
                Log.e("Success", obj.getResultCode() + "");
                if (obj.getResultCode() == 1003) {
                    String StrObject = (String) obj.getReturnObject();
                    Log.e("StrObject", StrObject + "");
                    Intent intent = new Intent();
                    intent.setAction(DefaultConsts.EXCEPTION_LOGIN_STRING);
                    intent.putExtra("json", "1003");
                    intent.putExtra("packageName", AppUtils.getPackageName(context));
                    context.sendBroadcast(intent);
                } else {
                    User user = (User) response.getReturnObject();
                    WorkUIKit.setUser(user);
                    loginTaiBaoListener.run();
                }
            }

            @Override
            public void onResponseFail(String tag, int resultCode) {
//                Util.showToast(context, "获取太保账号异常");
            }
        });
    }

    /**
     * 太保登录回调
     */
    public interface LoginTaiBaoListener {
        void run();
    }
}
